#pragma once
#include <array>
#include <fstream>
#include <functional>

using namespace std;

namespace U32
{

	class Statistics
	{
	private:
		array<int, 1000000> ar;
	public:

		Statistics()
		{
			Clear();
		}

		~Statistics()
		{
		}

		void Clear()
		{
			for (int i = 0; i < 1000000; i++)
			{
				ar[i] = 0;
			}
		}

		void ReadFromFile()
		{
			ifstream fin;
			fin.open("salary.txt");
			int kr;
			while (fin.good())
			{
				fin >> kr;
				if (kr > 20000 && kr < 1000000)
				{
					ar[kr]++;
				}
			}
		}

		int CalculateMean()
		{
			int noOfSalaries = 0;
			long long total = 0;
			for (int i = 0; i < 1000000; i++)
			{
				noOfSalaries += ar[i];
				if (ar[i] > 0)
				{
					total += i * ar[i];
				}
			}
			return total / noOfSalaries;
		}

		int CalculateMedian()
		{
			long long total = 0;
			for (int i = 0; i < 1000000; i++)
			{
				if (ar[i] > 0)
				{
					total += i * ar[i];
				}
			}
			long long halfTotal = total / 2;
			total = 0;
			for (int i = 0; i < 1000000; i++)
			{
				if (ar[i] > 0)
				{
					total += i * ar[i];
					if (total >= halfTotal)
						return i;
				}
			}
			return 0;
		}
	};
}

